﻿using LuceneWrap.Lib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using NewsMine.DomainObjects;
using Lucene.Net.Store;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Lucene.Net.Analysis.Standard;
using System.IO;

namespace LuceneWrapTest
{

    /// <summary>
    ///This is a test class for LuceneManagerTest and is intended
    ///to contain all LuceneManagerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LuceneManagerTest
    {


        [TestMethod()]
        public void AddWebSiteToIndex()
        {
            LuceneManager<WebSiteMenu> luceneManager = new LuceneManager<WebSiteMenu>(@"c:\temp\luceneWrapTestData\Index");

            for (int i = 1; i < 500000; i++)
            {
                luceneManager.AddObjectToIndex(new WebSiteMenu { ID = i, ModifiedTime= DateTime.UtcNow.AddHours(i), Name = "WebSiteInfo " + i.ToString(), Domain = "domain" + i.ToString() + ".com" });
            }

            //luceneManager.AddItemToIndex(new WebSiteInfo { Name = "test", Domain = "test.com", Language = "telugu", LocalName = "some localname", ID = 213, LastCrawledTimeStampGMT = DateTime.UtcNow.AddDays(-10) });
            luceneManager.FinalizeWriter(true);
        }

        [TestMethod()]
        public void GetWebSiteToIndex()
        {
            //LuceneManager<WebSiteInfo> luceneManager = new LuceneManager<WebSiteInfo>();

            // luceneManager.AddItemToIndex(new WebSiteInfo { Name = "test name ", Domain = "test.com", Language = "english", LocalName = "some localname" });

            LuceneSearcher searcher = new LuceneSearcher(@"c:\temp\luceneWrapTestData\Index");

            var result = searcher.Search<WebSiteInfo>(":2076");
            //result = searcher.Search<WebSiteInfo>("");

        }

        [TestMethod()]
        public void TestDateRange()
        {
            string _INDEX_FILEPATH = @"c:\temp\luceneWrapTestData\Index";
            Lucene.Net.Store.Directory _luceneIndexDirectory = null;
            IndexSearcher _indexSearcher = null;
            QueryParser _queryParser = null;
            StandardAnalyzer _analyzer = null;
            _luceneIndexDirectory = FSDirectory.Open(new DirectoryInfo(_INDEX_FILEPATH));
            _analyzer = new StandardAnalyzer();
            _indexSearcher = new IndexSearcher(_luceneIndexDirectory);



        }


        [TestMethod()]
        public void DateAndNumbericAndListOfStringTests()
        {
            if (true)
            {
                LuceneManager<WebSiteMenu> luceneManager = new LuceneManager<WebSiteMenu>(@"c:\temp\luceneWrapTestData\Index");
                WebSiteMenu websiteMenu = null;


                for (int i = 1; i < 500; i++)
                {
                    websiteMenu = new WebSiteMenu { ID = i, ModifiedTime = DateTime.UtcNow.AddHours(i), Name = "WebSiteInfo " + i.ToString(), Domain = "domain" + i.ToString() + ".com" };

                    if (i % 2 == 0)
                        websiteMenu.Tags.Add("even");
                    else
                        websiteMenu.Tags.Add("odd");

                    websiteMenu.Tags.Add("number");

                    websiteMenu.ID = i;

                    luceneManager.AddObjectToIndex(websiteMenu);
                }

                luceneManager.FinalizeWriter(true);
            }
            
            LuceneSearcher searcher = new LuceneSearcher(@"c:\temp\luceneWrapTestData\Index");

            var result = searcher.Search<WebSiteMenu>("modifiedtime:2012");
             result = searcher.Search<WebSiteMenu>("id:[2 TO 8]");
            //result = searcher.Search<WebSiteInfo>("");



        }

    }
}
